//
//  NetManager.m
//  UpNote
//
//  Created by Justsoso on 16/4/3.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "NetManager.h"

@implementation NetManager



/**
 *  查询网络笔记
 *
 *  @param successBlock 查询成功时执行的block
 */
+ (void)requestSearchNoteWith:(void (^)(NSArray *arr))successBlock
{
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com/service/mynotes/WebService.php?email=411051367@qq.com&type=JSON&action=query"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            successBlock(dict[@"Record"]);
        }
        else
        {
            NSLog(@"查询失败:%@",connectionError);
        }
    }];
}

/**
 *  添加笔记
 *
 *  @param content      笔记内容
 *  @param time         笔记时间
 *  @param successBlock 添加成功时执行的block
 */
+ (void)requestAddNoteWithContent:(NSString *)content
                          andTime:(NSString *)time
                     successBlock:(void (^)())successBlock
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=411051367@qq.com&type=JSON&action=add&content=%@&date=%@",content,time]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([dict[@"ResultCode"] isEqualToNumber:@1])
            {
                successBlock();
            }
        }
        else
        {
            NSLog(@"查询失败:%@",connectionError);
        }
    }];
    
}


/**
 *  删除笔记
 *
 *  @param noteId       笔记ID
 *  @param successBlock 删除成功时执行的block
 */
+ (void)requestRemoveNoteWithNoteId:(NSString *)noteId
                       successBlock:(void (^)())successBlock
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=411051367@qq.com&type=JSON&action=remove&id=%@",noteId]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([dict[@"ResultCode"] isEqualToNumber:@1])
            {
                successBlock();
            }
        }
        else
        {
            NSLog(@"查询失败:%@",connectionError);
        }
    }];
}
@end
